﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Cricket.LinqModel;
using Cricket.LinqData;

public partial class NineEleven : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    decimal countTotal1 = 0;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Client"];
            if (cookie == null)
                Response.Redirect("Login.aspx");
            else
                bindMatch();
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchesForReportResult> lstResult = new List<GetMatchesForReportResult>();
        lstResult = matchSchedule.GetMatchesForReport();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));

        }
    }
    private static DataTable GetData(string query)
    {
        string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Client"];
        if (cookie == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            grdAllCustomers.ToolTip = Convert.ToString(cookie["ClientId"]);
            grdAllCustomers.DataSource = GetData(string.Format("SELECT B.[Over], MSR.Runs as ActualRun, CASE CB.BetFor WHEN 'L' THEN B.BettingRate ELSE B.BettingRate END AS Runs, CB.Rate,CB.BetFor AS BettingFor,BettingAmount AS BettingAmount ,CONVERT(varchar(20),DATEADD(MI,30,DATEADD(hh,5,CB.CreatedDate)),113) As Date from Client C INNER JOIN CustomerBetting CB ON C.Id = CB.CreatedBy INNER JOIN FancyNineEleven B ON B.Id = CB.BettingId INNER JOIN MatchSessionResult MSR ON B.[Over] = MSR.[Over] AND B.[TeamId] = MSR.[TeamId] WHERE C.Id = '{0}' AND CB.MatchSchduleId = " + drpMatch.SelectedValue + " AND MSR.MatchScheduleId = " + drpMatch.SelectedValue + "  AND B.BettingTypeId = 5 ", Convert.ToInt32(cookie["ClientID"])));
            grdAllCustomers.DataBind();
        }
    }
    protected void grdAllCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblFinalAmount = (Label)e.Row.FindControl("lblFinalAmount");
            if (lblFinalAmount.Text != "")
            {
                countTotal1 = countTotal1 + Convert.ToDecimal(lblFinalAmount.Text);
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblFinalTotal = (Label)e.Row.FindControl("lblFinalTotal");
            lblFinalTotal.Text = countTotal1.ToString();
        }
    }
}